/*
 * © Copyright 2011 Romain Marteau, Matthieu Totet
 * 
 *    This file is part of VineaToum.
 *
 *    VineaToum is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    VineaToum is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with VineaToum.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.vineatoum.view;

import java.awt.BorderLayout;

import org.vineatoum.business.configuration.Appellation;
import org.vineatoum.business.configuration.AppellationType;
import org.vineatoum.business.configuration.Cepage;
import org.vineatoum.business.configuration.Commune;
import org.vineatoum.business.configuration.Configuration;
import org.vineatoum.business.configuration.LieuDit;
import org.vineatoum.business.configuration.MaturityMeasureType;
import org.vineatoum.business.configuration.OenologicalProductType;
import org.vineatoum.business.configuration.ProducerOeno;
import org.vineatoum.business.configuration.SoilType;
import org.vineatoum.business.configuration.CuveType;
import org.vineatoum.controller.ConfigurationController;
import org.vineatoum.controller.IConfigurationController;
import org.vineatoum.model.abstraction.IVineaToumModel;
import org.vineatoum.observer.IObserverConfiguration;
import org.vineatoum.view.component.JAccordion;
import org.vineatoum.view.data.AppellationDataPanel;
import org.vineatoum.view.data.LieuDitDataPanel;
import org.vineatoum.view.utils.JPanelWrapped;
import org.vineatoum.view.utils.jtablecustom.JTableCustom;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class ConfigurationPanel extends JPanelWrapped implements IObserverConfiguration {

	private static final long serialVersionUID = -1799140316981223703L;

	private final IVineaToumModel model;
	private final IConfigurationController controller;
	private final JAccordion mainPanel;
	
	final JTableCustom<Appellation> tableAppellation;
	final JTableCustom<AppellationType> tableAppellationType;
	final JTableCustom<Cepage> tableCepage;
	final JTableCustom<Commune> tableCommune;
	final JTableCustom<ProducerOeno> tableProducer;
	final JTableCustom<LieuDit> tableLieuDit;
	final JTableCustom<OenologicalProductType> tableProductOeno;
	final JTableCustom<CuveType> tableCuveType;
	final JTableCustom<SoilType> tableSoilType;
	final JTableCustom<MaturityMeasureType> tableMaturityType;
	
	final Configuration config;
	
	/**
	 * Create the panel.
	 */
	public ConfigurationPanel(final IVineaToumModel model) {
		super(new BorderLayout());
		this.model = model;
		this.model.addObserver(this);
		this.controller = new ConfigurationController(model);
		
		this.mainPanel = new JAccordion();
		
		config = model.getConfiguration();
		
		{
			JPanel appellation = new JPanel(new BorderLayout());

			tableAppellation = new JTableCustom<Appellation>(new Appellation()){
				private static final long serialVersionUID = 6718581644893264560L;

				protected boolean fireEditingStarted(final JTableCustom<Appellation> table, final int row, int column) {
					table.getCellEditor().stopCellEditing();
					final AppellationDataPanel appPanel = new AppellationDataPanel((Appellation) table.getValueAt(row), model);
					Object[] options = {"OK", "Annuler"};
					int n = JOptionPane.showOptionDialog(getRootPane(), appPanel, "Ajouter", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
					if(n == JOptionPane.OK_OPTION) {
						table.setValueAt(appPanel.getTextField().getText(), row, 0);
						table.setValueAt(appPanel.getComboBox().getSelectedItem(), row, 1);
					}
					
					return false;
				};
			};
			
			tableAppellation.setData(config.getAppellations());
			appellation.add(tableAppellation);
			appellation.add(tableAppellation.getTableHeader(), BorderLayout.NORTH);

			this.mainPanel.addBar("Appellations", appellation);
		}
		{
			JPanel appellationType = new JPanel(new BorderLayout());

			tableAppellationType = new JTableCustom<AppellationType>(new AppellationType());
			
			tableAppellationType.setData(config.getAppellationTypes());
			
			appellationType.add(tableAppellationType);
			appellationType.add(tableAppellationType.getTableHeader(), BorderLayout.NORTH);
			this.mainPanel.addBar("Types d'Appellation", appellationType);
		}
		{
			JPanel cepage = new JPanel(new BorderLayout());
			
			tableCepage = new JTableCustom<Cepage>(new Cepage());
			tableCepage.setData(config.getCepages());
			
			cepage.add(tableCepage);
			cepage.add(tableCepage.getTableHeader(), BorderLayout.NORTH);
			this.mainPanel.addBar("Cépages", cepage);
		}
		{
			JPanel commune = new JPanel(new BorderLayout());
			
			tableCommune = new JTableCustom<Commune>(new Commune());
			tableCommune.setData(config.getCommunes());
			
			commune.add(tableCommune);
			commune.add(tableCommune.getTableHeader(), BorderLayout.NORTH);
			this.mainPanel.addBar("Communes", commune);
		}
		{
			JPanel lieuDit = new JPanel(new BorderLayout());

			
			tableLieuDit = new JTableCustom<LieuDit>(new LieuDit()) {
				private static final long serialVersionUID = 6718581644893264560L;

				protected boolean fireEditingStarted(final JTableCustom<LieuDit> table, final int row, int column) {
					table.getCellEditor().stopCellEditing();
					final LieuDitDataPanel appPanel = new LieuDitDataPanel((LieuDit) table.getValueAt(row));
					Object[] options = {"OK", "Annuler"};
					int n = JOptionPane.showOptionDialog(getRootPane(), appPanel, "Ajouter", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
					if(n == JOptionPane.OK_OPTION) {
						table.setValueAt(appPanel.getTextField().getText(), row, 0);
						table.setValueAt(appPanel.getComboBox().getSelectedItem(), row, 1);
					}
					
					return false;
				};
			};
			tableLieuDit.setData(config.getLieuDits());
			
			lieuDit.add(tableLieuDit);
			lieuDit.add(tableLieuDit.getTableHeader(), BorderLayout.NORTH);
			this.mainPanel.addBar("Lieux-Dits", lieuDit);
		}
		{
			JPanel producerOeno = new JPanel(new BorderLayout());
			
			tableProducer = new JTableCustom<ProducerOeno>(new ProducerOeno());
			tableProducer.setData(config.getProducerOenos());
			
			producerOeno.add(tableProducer);
			producerOeno.add(tableProducer.getTableHeader(), BorderLayout.NORTH);
			this.mainPanel.addBar("Producteurs de produits oenologiques", producerOeno);
		}
		{
			JPanel productOeno = new JPanel(new BorderLayout());
			
			tableProductOeno = new JTableCustom<OenologicalProductType>(new OenologicalProductType());
			tableProductOeno.setData(config.getProductOenoTypes());
			
			productOeno.add(tableProductOeno);
			productOeno.add(tableProductOeno.getTableHeader(), BorderLayout.NORTH);
			this.mainPanel.addBar("Types de produits oenologiques", productOeno);
		}
		{
			JPanel cuveType = new JPanel(new BorderLayout());
			
			tableCuveType = new JTableCustom<CuveType>(new CuveType());
			tableCuveType.setData(config.getCuveTypes());
			
			cuveType.add(tableCuveType);
			cuveType.add(tableCuveType.getTableHeader(), BorderLayout.NORTH);
			this.mainPanel.addBar("Types de cuve", cuveType);
		}
		{
			JPanel soilType = new JPanel(new BorderLayout());
			
			tableSoilType = new JTableCustom<SoilType>(new SoilType());
			tableSoilType.setData(config.getSoilTypes());
			
			soilType.add(tableSoilType);
			soilType.add(tableSoilType.getTableHeader(), BorderLayout.NORTH);
			this.mainPanel.addBar("Types de sol", soilType);
		}
		{
			JPanel maturityMeasurementType = new JPanel(new BorderLayout());
			
			tableMaturityType = new JTableCustom<MaturityMeasureType>(new MaturityMeasureType());
			tableMaturityType.setData(config.getMaturityMeasureTypes());
			
			maturityMeasurementType.add(tableMaturityType);
			maturityMeasurementType.add(tableMaturityType.getTableHeader(), BorderLayout.NORTH);
			this.mainPanel.addBar("Types de mesure de maturité", maturityMeasurementType);
		}
		
		add(this.mainPanel, BorderLayout.CENTER);
		
	}

	@Override
	public void doWhenWrapperCancelClick() {
		// TODO Auto-generated method stub
	}

	@Override
	public void doWhenWrapperOkClick() {
		config.setAppellation(tableAppellation.getData());
		config.setAppellationType(tableAppellationType.getData());
		config.setCepage(tableCepage.getData());
		config.setCommune(tableCommune.getData());
		config.setProducerOeno(tableProducer.getData());
		config.setLieuDit(tableLieuDit.getData());
		config.setProductOeno(tableProductOeno.getData());
		config.setCuveType(tableCuveType.getData());
		config.setSoilType(tableSoilType.getData());
		config.setMaturityMeasureType(tableMaturityType.getData());
		
		controller.updateConfiguration(config);
	}

}
